package com.hailian.fintech.hlfinanceserver.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hailian.fintech.hlcomm_interfaceserver.dto.BaoliDto;
import com.hailian.fintech.hlcomm_interfaceserver.dto.YingyeDto;
import com.hailian.fintech.hlcomm_interfaceserver.result.ResultInfo;
import com.hailian.fintech.hlfinanceserver.entity.Yingye;
import com.hailian.fintech.hlfinanceserver.service.IYingyeService;
import com.hailian.fintech.hlfinanceserver.vo.YingyeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.beans.IntrospectionException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wk
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/yingye")
@Slf4j
public class YingyeController {
    @Autowired
    private IYingyeService iYingyeService;

    @RequestMapping("list")
    public Object list(){
        return new ResultInfo().success(iYingyeService.list());
    }

    @RequestMapping("/downLoadYingYeExcel")
    public ResponseEntity<byte[]> downLoadGoodsExcel(YingyeDto yingyeDto) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException, IOException, IOException {
        //log.info("开始打印导出excel 的 日志....");
        XSSFWorkbook xssfSheets = iYingyeService.downLoadGoodsExcel(yingyeDto);
        //log.info("--------------------------------结果是{}:{}",xssfSheets,1);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xssfSheets.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("保理信息表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

    }

    @RequestMapping("/findAll")
    public ResultInfo findAll(@RequestParam(defaultValue = "1") long current,
                                   @RequestParam(defaultValue = "3") long size,
                                   YingyeVo yingyeVo){
        //log.info("{}"+yingyeVo.getStatetime() +"<==================>"+ yingyeVo.getEndtime());
        Page<YingyeVo> page = new Page<>(current, size);
        return new ResultInfo().success(iYingyeService.selectPageVo(page, yingyeVo));
    }

}

